import { getProductList, addProduct, updateProduct, getProductById, deleteProductById, addProductAndSubmit, updateProductAndSubmit, copyProductById } from '../../services/outbuy/outbuyProduct';
import {message} from 'antd';
const R=require("ramda");
import OperatorConstant from "../../utils/OperatorConstant";

export default {
    namespace: 'outbuyProduct',
    state: {
        page: {},
        list: [],
        dataSource: {}
    },
    reducers: {
        /**
         * 刷新state
         *
         * @param state
         */
        refreshState(state, { payload }){
            return {...state, ...payload };
        }
    },
    effects: {
        /**
         * 获取产品信息列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getProductList({payload}, { call, put, select }) {
            const res = yield call(getProductList, { ...payload });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'refreshState',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         * 增加产品信息
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *addProduct({payload}, { call, put, select }) {
            let method = R.isNil(payload.method) ? addProduct : (payload.method == OperatorConstant.saveAndApproval ? addProductAndSubmit : addProduct);
            const res = yield call(method, {...payload});

            if (res.data.resultCode === "ok") {
                message.info("增加产品成功");
                if(payload.onSuccess)payload.onSuccess();

            } else {
                if(payload.onFail)payload.onFail();
                message.error(res.data.errMessage);
            }
        },


        /**
         * 更新产品信息
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *updateProduct({payload}, { call, put, select }) {
            let method = R.isNil(payload.method) ? updateProduct : (payload.method == OperatorConstant.saveAndApproval ? updateProductAndSubmit : updateProduct);
            const res = yield call(method, {...payload});

            if (res.data.resultCode === "ok") {
                message.info("修改产品成功");
                if(payload.onSuccess)payload.onSuccess();

            } else {
                if(payload.onFail)payload.onFail();
                message.error(res.data.errMessage);
            }
        },

        /**
         * 根据id查询产品信息
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *getProductById({ payload }, { call, put, select }) {
            const res = yield call(getProductById, {id: payload.id});

            if (res.data.resultCode === "ok") {
                yield put({
                    type: "refreshState",
                    payload: {
                        dataSource: res.data.content,
                    }
                });
            }
        },

        /**
         * 删除产品信息
         * @param payload
         * @param call
         * @param put
         * @param select
         */
            *deleteProductById({ payload }, { call, put, select }) {
            const res = yield call(deleteProductById, {id: payload.id})

            if (res.data.resultCode === "ok") {
                message.info("删除产品成功");
                if(payload.onSuccess)payload.onSuccess();
            } else {
                message.error(res.data.errMessage);
            }
        },

        /**
         *
         * 复制产品
         * @param payload
         * @param call
         * @param put
         * @param select
         *
         */
            *copyProudctById({ payload }, { call, put, select }) {
            const res = yield call(copyProductById, { id: payload.id });
            if (res.data.resultCode === "ok") {
                message.info("复制成功！");
            } else {
                message.error(res.data.errMessage)
            }
        },

    }
};
